
puts "MATH PROBLEMS"
puts "  by Justin M Wozniak <justin@wozniaks.org>"
puts ""

while 1 {
    puts -nonewline "How many problems? "
    flush stdout
    gets stdin line
    if [ string is integer $line ] {
	break
    }
    puts ""
}

puts [ format "Doing %s problems." $line ]

set problems $line

proc addition_problem { } {

    set x [ expr round(rand()*9) ]
    set y [ expr round(rand()*9) ]

    while 1 {

	puts -nonewline " $x + $y = "
	flush stdout
	gets stdin line
	if { $x+$y == $line } {
	    puts "GOOD!\n\n"
	    break
	} else {
	    puts "WRONG"
	}
    }
}

proc multiplication_problem { } {

    set x [ expr round(rand()*9) ]
    set y [ expr round(rand()*9) ]


    while 1 {
	puts -nonewline " $x x $y = "
	flush stdout
	gets stdin line
	after 1000
	if { $x*$y == $line } {
	    puts "GOOD!\n\n"
	    break
	} else {
	    puts "WRONG"
	}
    }
}

for { set i 0 } { $i < $problems } { incr i } {

	addition_problem

	multiplication_problem
	puts $i
}
